package switchviz.core.view;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;

import switchviz.core.signal.ISignal;
import switchviz.graphic.GraphicFactory;
import switchviz.graphic.gauge.GaugeNodeModel;
import switchviz.graphic.gauge.GaugePersistence;
import switchviz.graphic.gauge.IGauge;
import switchviz.util.FileOperations;

public class View 
{
	private ViewDataModel _datamodel = null;
	private IGauge[] _gaugearray = null;
	private List<ISignal> _signallist = null;
	private List<String> _portnum = null;
	
	public void setPortNum(List<String> portnum)
	{
		_portnum = portnum;
	}
	
	public List<String> getPortNum()
	{
		return _portnum;
	}
	
	public List<ISignal> getSignalList()
	{
		if(_signallist == null)
			_signallist = getAllSignals();
		return _signallist;
	}
	
	public void setDataModel(ViewDataModel datamodel) throws NullPointerException
	{
		if (datamodel == null) { throw new NullPointerException(); }
		_datamodel = datamodel;
	}
	
	public ViewDataModel getDataModel()
	{
		return _datamodel;
	}
	
	public View(ViewDataModel datamodel) throws NullPointerException
	{
		this.setDataModel(datamodel);
	}
	
	public IGauge[] getGaugeArray()
	{
		if (_gaugearray == null)
			_gaugearray = getNewGaugeArray();

		return _gaugearray;
	}
	
	public void setGaugeArray(IGauge[] gaugearray)
	{
		_gaugearray = gaugearray;
	}
	
	public void saveCurrentView() throws IOException
	{
		saveCurrentViewAs(getDataModel().getFile());
	}
	
	public void saveCurrentViewAs(File file) throws IOException
	{
		getDataModel().setName(file.getName());
		Element root = ViewTree.createNewViewNodes(getDataModel());
		for(int i = 0; i < getGaugeArray().length; ++i)
		{
			root.addContent(GaugePersistence.setMultiplePerPorts((Element)getGaugeArray()[i].getElement().clone(), false, getGaugeArray()[i].getGaugeDataModel().getPort()));
		}
		Document doc = new Document(root);
		FileOperations.saveXML(file, doc);
	}
	
	private IGauge[] getNewGaugeArray()
	{
		IGauge[] retarray = new IGauge[this.getDataModel().getGaugeConfList().size()];
		List<IGauge> gaugelist = new ArrayList<IGauge>();
		for(int i = 0; i < this.getDataModel().getGaugeConfList().size(); ++i)
		{
			GaugeNodeModel gm = this.getDataModel().getGaugeConfList().get(i);
			IGauge gauge = GraphicFactory.getInstance().getGauge(gm.getType(), (Element)gm.getElement().clone());
			if(gauge.getGaugeDataModel().isMultiplePerPort())
			{
				gaugelist.add(gauge);
				if(_portnum != null)
				{
					if(_portnum.size() < 1 || _portnum.get(0) == null) continue;
					
					gauge.getGaugeDataModel().setPort(Integer.valueOf(_portnum.get(0)));
					for(int j = 1; j < _portnum.size(); ++j)
					{
						if(_portnum.get(j) == null) continue;
						IGauge newgauge = GraphicFactory.getInstance().getGauge(gm.getType(), (Element)gm.getElement().clone());
						newgauge.getGaugeDataModel().setPort(Integer.valueOf(_portnum.get(j)));
						gaugelist.add(newgauge);
					}
				}
			}
			else
			{
				gaugelist.add(gauge);
			}
		}
		
		return gaugelist.toArray(retarray);
	}
	
	private List<ISignal> getAllSignals()
	{
		List<ISignal> signallist = new ArrayList<ISignal>();
		
		if(getGaugeArray() == null) return null;
		
		for(int i = 0; i < getGaugeArray().length; ++i)
		{
			for(int j = 0; j < getGaugeArray()[i].getSignals().length; ++j)
			{
				if(getGaugeArray()[i].getGaugeDataModel().isMultiplePerPort())
					getGaugeArray()[i].getSignals()[j].setPort(getGaugeArray()[i].getGaugeDataModel().getPort());
				
				signallist.add(getGaugeArray()[i].getSignals()[j]);
			}
		}
		return signallist;
	}
	
	public void setFreeze(boolean b)
	{
		for(int i = 0; i < getGaugeArray().length; ++i)
		{
			getGaugeArray()[i].setFreeze(b);
		}
	}
}
